package br.com.projetoia.dama.main;

import java.util.Scanner;

import br.com.projetoia.dama.BestMovement;
import br.com.projetoia.dama.Board;
import br.com.projetoia.dama.GameManagement;
import br.com.projetoia.dama.MiniMax;
import br.com.projetoia.dama.Position;
import br.com.projetoia.dama.enums.WalkerTypeEnum;

public class ExecuteGame {

	private final GameManagement gameManagement;
	private final Board board;
	private final MiniMax miniMax;

	public ExecuteGame() {
		this.gameManagement = new GameManagement();
		this.board = new Board();
		this.board.create();

		this.gameManagement.setBoard(this.board);

		this.miniMax = new MiniMax(this.board);

	}

	public void startGame() {
		Scanner input = new Scanner(System.in);

		boolean hasWinner = false;

		while (!hasWinner) {
			this.gameManagement.getBoard().printBoard();

			System.out.println("Selecione a Peça");

			int houseLine = input.nextInt();
			int houseColumn = input.nextInt();

			if (houseLine == -1) {
				break;
			}

			try {
				this.gameManagement.selectHouse(houseLine, houseColumn);

				System.out.println("Digite a casa de destino;");

				int line = input.nextInt();
				int column = input.nextInt();

				this.gameManagement.moveWalkerTo(line, column);

				BestMovement bestMovement = this.miniMax.max(WalkerTypeEnum.BLACK, this.board, 1);

				Position source = bestMovement.getSource();
				this.gameManagement.selectHouse(source.getLine(), source.getColumn());

				Position destination = bestMovement.getDestination();
				this.gameManagement.moveWalkerTo(destination.getLine(), destination.getColumn());

			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}
}
